package mvp.cklm.testtask.main;

import mvp.cklm.testtask.model.AddressModel;
import mvp.cklm.testtask.model.enums.AddressRequest;
import mvp.cklm.testtask.service.IAddressService;
import mvp.cklm.testtask.util.FakeAddressCreator;
import mvp.cklm.testtask.util.ServiceKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: valera
 * Date: 25.02.14
 * Time: 10:56
 */
public class Demo {
    private static final Logger log = LoggerFactory.getLogger(Demo.class);
    private IAddressService addressService;

    public Demo() {
        addressService = (IAddressService) ServiceKeeper.getService(IAddressService.class);
    }

    public void demo() {
        fill();
        request();
    }

    private void fill() {
        FakeAddressCreator creator = new FakeAddressCreator();
        Random r = new Random();
        for (int i = 0; i < r.nextInt(100); i++) {
            addressService.saveAddress(creator.createAddress(addressService.getNextAddressId()));
        }
    }

    private void request() {
        long address = addressService.getAddressCount();
        log.info("Now in DB {} addresses", address);
        Map<AddressRequest, Object> reqMap = new HashMap<AddressRequest, Object>();
        reqMap.put(AddressRequest.COUNTRY, "Germany");
        List<AddressModel> fromGermany = addressService.getAddressesByRequest(reqMap);
        int fromGermanySize = fromGermany.size();
        log.info("From Germany {} ", fromGermanySize);
    }

}
